package com.itheima.service;

import com.itheima.mapper.StuMapper;
import com.itheima.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StuService {
    public boolean checkExists(String name,String password) throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 执行sql
        // 3.1 获取userMapper接口代理对象
        StuMapper stuMapper = sqlSession.getMapper(StuMapper.class);
        Student student = stuMapper.login(name,password);
        //4. 释放资源
        sqlSession.close();
        return student != null ;
    }

    public boolean checkExists(String username) {
        return false;
    }
}
